<template>
  <div class="cart">
    <!-- 购物车顶部 -->
    <van-nav-bar title="购物车">
      <i slot="left" class="iconfont icon-zuojiantou"></i>
      <template #right>
        <span class="m-r-20" v-if="!isManage" @click="isManage=true">管理</span
        >
        <span v-if="!isManage">分享</span>
        <span v-else @click="isManage=false">完成</span>
      </template>
    </van-nav-bar>
    <van-empty description="暂无商品" v-if="!cartList.length">
      <van-button round type="danger" to="/home">
        立即选购
      </van-button>
    </van-empty>
    <!-- 购物车单项 -->
    <div class="cart-item" v-for="(item, index) in cartList" :key="index">
      <div class="checkbox" @click="handleCheck(index)">
        <i class="iconfont icon-gou" v-if="item.ischeck"></i>
        <i class="iconfont icon-big-circle" v-else></i>
      </div>
      <div class="content">
        <div class="img">
          <img :src="item.image_url" alt="" />
        </div>
        <div class="details">
          <div class="title">
            <h2>{{item.name}}{{item.count}}</h2>
          </div>
          <div class="material">
            <h3>2360*1680*1020（1.5*2.0米）进口头层牛皮（接触面真皮）米白色</h3>
          </div>
          <div class="price-count">
            <p>
              <span>￥</span><span>{{item.prices[0].price}}</span>
            </p>
            <div class="count">
              <van-stepper v-model="item.count" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 购物车底部 -->
    <div class="settlement" v-if="!isManage">
      <div class="box">
        <div class="checkall" @click="handleAllCheck(allInfo.allCheck)">
          <i class="iconfont icon-gou" v-if="allInfo.allCheck"></i>
          <i class="iconfont icon-big-circle" v-else></i>
          <span>全选</span>
        </div>
        <div class="count-empty">
          <span>总计:</span><span>￥{{allInfo.checkNum}}</span>
        </div>
        <div class="count">
          <p>
            <span>总计:</span><span>￥{{allInfo.checkPrice}}</span>
          </p>
          <!-- <p><span>原价:￥3720 已省:</span><span>￥730</span></p> -->
        </div>
      </div>
      <button>
        <span>
          结算
          <i>8</i>
        </span>
      </button>
    </div>
    <div class="edit" v-else>
      <div class="editbox">
        <div class="checkall" @click="handleAllCheck(allInfo.allCheck)">
          <i class="iconfont icon-gou" v-if="allInfo.allCheck"></i>
          <i class="iconfont icon-big-circle" v-else></i>
          <span>全选</span>
        </div>
      </div>
      <div class="buttons">
        <p>移入收藏</p>
        <span @click="handleDel">删除</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from '@vue/runtime-core'
import {useStore} from 'vuex'
let store = useStore()
let cartList = computed(()=>{
  return store.state.cart.cartList
})
let handleCheck = (idx)=>{
  store.commit('cart/cartCheckMut',idx)
}
let allInfo = computed(()=>{  //动态获取getters
  return store.getters['cart/allInfo']
})
let handleAllCheck = (bool)=>{ //全选
  store.commit('cart/cartAllCheckMut',bool)
}

let isManage = ref(false) //是否管理购物车

let handleDel = ()=>{  //删除
  store.commit('cart/cartDelMut')
}
</script>

<style lang="less" scoped>
@color: #be4c4b;
/deep/.van-nav-bar .van-icon,
/deep/.van-nav-bar__text {
  color: #000;
}
/deep/.van-nav-bar {
  position: fixed;
  top: 0;
  width: 100%;
}
.cart {
  // background: #efefef;
  height: 100%;
  padding: 9.2rem 0 26rem 0;
  // 购物车单项
  .cart-item {
    width: 100%;
    height: 24rem;
    background: #fff;
    margin-top: 2rem;
    display: flex;
    align-items: center;
    .checkbox {
      padding: 2rem 0 2rem 3rem;
      i {
        color: #b1b1b1;
        margin-right: 3rem;
      }
      .icon-gou {
        color: @color;
      }
    }
    .content {
      display: flex;
      .img {
        width: 18rem;
        height: 18rem;
        margin-right: 2rem;
        img{
          height: 100%;
          width: 100%;
        }
      }
      .details {
        padding-right: 3rem;
        .title {
          font-size: 2.8rem;
          font-family: Helvetica, SimHei;
          margin-bottom: 1rem;
        }
        .material {
          color: #7d7d7d;
          line-height: 3rem;
          width: 42rem;
          margin-bottom: 1rem;
          h3 {
            font-size: 2.4rem;
            font-family: Helvetica, SimHei;
          }
        }
        .price-count {
          display: flex;
          justify-content: space-between;
          align-items: center;
          span {
            font-size: 3.2rem;
            color: @color;
          }
          .count {
            display: flex;
            align-items: center;
            input {
              width: 8rem;
              height: 5rem;
              background: #f6f6f6;
              text-align: center;
              margin: 0 0.5rem;
              font-size: 2.8rem;
            }
            .button {
              width: 4.6rem;
              height: 5rem;
              background: #f6f6f6;
              font-size: 3.5rem;
              line-height: 5rem;
              display: flex;
              align-items: center;
              justify-content: center;
            }
          }
        }
      }
    }
  }
  // 购物车底部
  .settlement {
    height: 12rem;
    background: #fff;
    position: fixed;
    bottom: 12rem;
    padding: 0 3rem;
    display: flex;
    align-items: center;
    .box {
      display: flex;
      width: 45rem;
      height: 100%;
      align-items: center;
      justify-content: space-between;
      padding-right: 3rem;
      .checkall {
        i {
          color: #b1b1b1;
          margin-right: 1rem;
        }
        .icon-gou {
          color: @color;
        }
        span {
          font-family: Helvetica, SimHei;
          font-size: 2.8rem;
        }
      }
      .count-empty {
        span {
          font-size: 3.2rem;
        }
        span:nth-of-type(2) {
          color: @color;
        }
      }
      .count {
        display: flex;
        flex-direction: column;
        p:nth-of-type(1) {
          align-self: flex-end;
          span {
            font-size: 3.2rem;
          }
          span:nth-of-type(2) {
            color: @color;
          }
        }
        p:nth-of-type(2) {
          align-self: flex-end;
          span {
            font-size: 2.4rem;
          }
          span:nth-of-type(2) {
            color: @color;
          }
        }
      }
    }
    button {
      width: 24rem;
      height: 8.8rem;
      background: @color;
      border-radius: 4.4rem;
      font-size: 3.2rem;
      color: #fff;
    }
  }
  .edit {
    height: 12rem;
    background: #fff;
    position: fixed;
    bottom: 12rem;
    padding: 0 3rem;
    display: flex;
    align-items: center;
    .editbox {
      display: flex;
      width: 24rem;
      height: 100%;
      align-items: center;
      justify-content: space-between;
      padding-right: 3rem;
      .checkall {
        i {
          color: #b1b1b1;
          margin-right: 1rem;
        }
        .icon-gou {
          color: @color;
        }
        span {
          font-family: Helvetica, SimHei;
          font-size: 2.8rem;
        }
      }
    }
    .buttons {
      display: flex;
      align-items: center;
      p {
        width: 25rem;
        height: 8.8rem;
        border-radius: 4.4rem;
        border: 1px solid @color;
        font-size: 3.2rem;
        color: @color;
        text-align: center;
        line-height: 8.8rem;
        margin-right: 2rem;
      }
      span {
        width: 18.4rem;
        height: 8.8rem;
        background: @color;
        border-radius: 4.4rem;
        font-size: 3.2rem;
        color: #fff;
        text-align: center;
        line-height: 8.8rem;
        margin-right: 3rem;
      }
    }
  }
}

.icon-zuojiantou {
  font-size: 3.8rem;
}
</style>